package com.example.springbootweb_comprehensiveexample.service.impl;

import com.example.springbootweb_comprehensiveexample.service.EmpService;
import com.example.springbootweb_comprehensiveexample.mapper.EmpMapper;
import com.example.springbootweb_comprehensiveexample.pojo.Emp;
import com.example.springbootweb_comprehensiveexample.pojo.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    //查询员工
    /*@Override
    public PageBean page(Integer page, Integer pageSize) {

        Long count = empMapper.count();

        Integer start = (page - 1) * pageSize;
        List<Emp> empList = empMapper.page(start, pageSize);

        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }*/
    //使用pagehelper
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

    //1.设置分页参数
        PageHelper.startPage(page, pageSize);
    //2.执行查询
        List<Emp> emplist = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) emplist;
    //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

        return pageBean;
    }

    //删除员工
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    //添加员工
    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    //根据id查询员工
    @Override
    public Emp selectById(Integer id) {
        return empMapper.selectById(id);
    }

    //更新员工信息
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    //登录
    @Override
    public Emp login(Emp emp) {
        return empMapper.getUsernameAndPassword(emp);
    }

    //退出登录

    @Override
    public void logout(String username, String password) {
        empMapper.logout(username, password);
    }
}
